var fs = require('fs');
var request = require("request");

function download(url,name){
    try{
        console.log('name ==> '+ name);
        console.log('url ==> '+ url);
        if(!url){
            fs.writeFile('error-log.txt', `${name} ===> img url is null\n`,{flag: "a"},()=>{})
            return
        }
        if(url.trim()=="" || url == "undefined"){
            fs.writeFile('error-log.txt', `${name} ===> img url is null\n`,{flag: "a"},()=>{})
            return
        }
        var writeStream = fs.createWriteStream(`images/${name}.jpg`);
        var readStream = request(url)
        readStream.pipe(writeStream);
        readStream.on('end', function() {
            console.log('文件下载成功 ==> '+ url);
        });
        readStream.on('error', function() {
            console.log('下载失败 ==> '+ url + " " + name);
            console.log("错误信息:" + err)
        })
        writeStream.on("finish", function() {
            console.log("文件写入成功");
            writeStream.end();
        });
    }catch(e){
        let err_str = e.message
        console.log(err_str);
        fs.writeFile('error-log.txt', `${name} ===> ${err_str}\n`,{flag: "a"},()=>{})
    }
}

//--------------------
let dir = 'images/'
if(!fs.existsSync(dir)){
    fs.mkdirSync(dir)    
}

fs.writeFileSync('error-log.txt', "",{flag: "w"})
var f_str = fs.readFileSync('./data.csv',{encoding : 'utf8'})
var rows = f_str.split('\n')
rows.shift()

rows.forEach((item)=>{
    let row_data = item.split(',')
    setTimeout(()=>{
        download(row_data[1], row_data[0])
    },200)
})
